<?php

declare(strict_types=1);

namespace App\Game\Model;

use App\Fastapp\Model\Account;
use App\Fastapp\Model\AccountInviteLevel;
use App\Fastapp\Model\AccountRecharge;
use App\Fastapp\Model\AccountSetting;
use App\Fastapp\Model\AccountWallet;
use App\Fastapp\Model\AccountWalletLog;
use App\Fastapp\Model\AccountWithdrawal;
use Carbon\Carbon;
use Hyperf\Database\Model\Relations\HasOne;

/**
 * @property int $id 主键
 * @property int $user_id 用户id
 * @property int $parent_id 上级id
 * @property string $link 链条
 * @property \Carbon\Carbon $created_at 创建时间
 * @property int $status
 * @property-read null|Account $account
 */
class GameAccount extends AccountInviteLevel
{
    public function setting(): HasOne
    {
        return $this->hasOne(AccountSetting::class, 'user_id', 'user_id')->where(['key' => 'agent_demo_balance']);
    }

    public function wallet(): HasOne
    {
        return $this->hasOne(AccountWallet::class, 'user_id', 'user_id')->orderBy('id');
    }

    public function level(): HasOne
    {
        return $this->hasOne(GameAccountVip::class, 'user_id', 'user_id');
    }

    public function recharge(): HasOne
    {
        return $this->hasOne(AccountRecharge::class, 'user_id', 'user_id')
            ->where(['status' => 1])
            ->selectRaw('user_id,sum(amount) as amount')->groupBy('user_id')->withDefault();
    }

    public function withdrawal(): HasOne
    {
        return $this->hasOne(AccountWithdrawal::class, 'user_id', 'user_id')
            ->whereIn('status', [0, 1])
            ->selectRaw('user_id,sum(amount) as amount')->groupBy('user_id')->withDefault();
    }

    public function bet(): HasOne
    {
        return $this->hasOne(GameBet::class, 'user_id', 'user_id')
            ->where('status', '>', 0)
            ->selectRaw('user_id,sum(amount) as amount')->groupBy('user_id')->withDefault();
    }

    public function children(): HasOne
    {
        return $this->hasOne(AccountInviteLevel::class, 'parent_id', 'user_id')
            ->selectRaw('user_id,count(*) as num')->groupBy('user_id')->withDefault();
    }

    public function parent(): HasOne
    {
        return $this->hasOne(Account::class, 'gene_id', 'parent_id');
    }

    public function transaction(): HasOne
    {
        return $this->hasOne(AccountWalletLog::class, 'user_id', 'user_id')
            ->selectRaw('SUM(amount) as amount,user_id,currency_id')
            ->where(['type' => AccountWalletLog::USE_BALANCE, 'field' => 'balance'])
            ->groupBy(['user_id', 'currency_id'])->withDefault();
    }

    public function profit(): HasOne
    {
        return $this->hasOne(AccountWalletLog::class, 'user_id', 'user_id')
            ->selectRaw('SUM(amount) as amount,user_id,currency_id')
            ->where(['field' => 'balance', 'type' => AccountWalletLog::RECHARGE])
            ->whereIn('object_type', [71, 3, 4, 6])
            ->groupBy(['user_id', 'currency_id'])->withDefault();
    }

    public function transactionToday(): HasOne
    {
        $t = Carbon::now();
        return $this->hasOne(AccountWalletLog::class, 'user_id', 'user_id')
            ->selectRaw('SUM(amount) as amount,user_id,currency_id')
            ->where(['type' => AccountWalletLog::USE_BALANCE, 'field' => 'balance'])
            ->whereBetween('created_at', [$t->toDateString() . ' 00:00:00', $t->addDay()->toDateString() . ' 00:00:00'])
            ->groupBy(['user_id', 'currency_id'])->withDefault();
    }

    public function profitToday(): HasOne
    {
        $t = Carbon::now();
        return $this->hasOne(AccountWalletLog::class, 'user_id', 'user_id')
            ->selectRaw('SUM(amount) as amount,user_id,currency_id')
            ->where(['field' => 'balance', 'type' => AccountWalletLog::RECHARGE])
            ->whereIn('object_type', [71, 3, 4, 6])
            ->whereBetween('created_at', [$t->toDateString() . ' 00:00:00', $t->addDay()->toDateString() . ' 00:00:00'])
            ->groupBy(['user_id', 'currency_id'])->withDefault();
    }
}
